﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace plugin.collctr.scsmdw
{
    public partial class PSCode : Form
    {
        public PSCode()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void PSCode_Load(object sender, EventArgs e)
        {
            string sCurrentDir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            try
            {
                LoadPSFolders(new DirectoryInfo(Path.Combine(sCurrentDir, "SCSMDWScripts")), treeView1.Nodes);
                treeView1.ExpandAll();
            }
            catch(Exception ex)
            {
                ex.Message.ToString();
            }
        }

        private void LoadPSFolders(DirectoryInfo Dir, TreeNodeCollection tnc)
        {
            foreach (string sFile in Directory.GetFiles(Dir.FullName, "*.ps1", SearchOption.TopDirectoryOnly))
            {
                TreeNode tn = new TreeNode();
                tn.ImageIndex = 1;
                tn.SelectedImageIndex = 1;
                tn.Text = Path.GetFileNameWithoutExtension(new FileInfo(sFile).FullName);
                tn.Tag = sFile;
                tn.Name = sFile;

                tnc.Add(tn);

            }

            foreach (string sDir in Directory.GetDirectories(Dir.FullName, "*", SearchOption.TopDirectoryOnly))
            {
                TreeNode tnFolder = new TreeNode();
                tnFolder.ImageIndex = 0;
                tnFolder.SelectedImageIndex = 0;
                tnFolder.Text = new DirectoryInfo(sDir).Name;
                tnFolder.Name = sDir;


                LoadPSFolders(new DirectoryInfo(sDir), tnFolder.Nodes);

                tnc.Add(tnFolder);
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView1.SelectedNode == null)
                button1.Enabled = false;
            else
            {
                if (treeView1.SelectedNode.ImageIndex == 0)
                    button1.Enabled = false;
                else
                {
                    try
                    {
                        string sFile = treeView1.SelectedNode.Tag.ToString();
                        sFile.ToString();

                        StreamReader streamReader = new StreamReader(sFile, Encoding.UTF8);
                        string text = streamReader.ReadToEnd();
                        streamReader.Close();
                        this.Tag = text;
                    }
                    catch { }
                    button1.Enabled = true;
                }

            }
        }
    }
}
